import {cart_index,cart_delete,cart_checked,cart_update} from "../../request/api"
// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList:[] ,// 商品总列表
    cartTotal:{} ,// 购物车数据
    checked:"" ,// 全选
    show:false, // 编辑
  },
  // 全选
  onChange_all(event) {
    this.setData({checked:!this.data.checked})
    let newarr = this.data.cartList.map((item,index)=>{
      return item.product_id
    })
    let productIds = newarr.join();
    let isChecked = event.detail?1:0;
    this.cartChecked(isChecked,productIds);
    console.log(event.detail);

  },
  // 切换商品选中状态
  async cartChecked(isChecked,productIds){
    let res = await cart_checked({data:{isChecked,productIds},method:"POST"})
    console.log(res);
    let {cartList} = res.data.data;
    this.setData({
      cartList
    })
    for (let i = 0; i < cartList.length; i++) {
      if (!cartList[i].checked) {
        this.setData({checked:false})
        break
      }else{
        this.setData({checked:true})
    }
    }
  },
  // 步进器编辑
  async onChange_step(v){
    console.log(v);
    let goodsId = v.currentTarget.dataset.goodsid;
    let id = v.currentTarget.dataset.id;
    let productId = v.currentTarget.dataset.productid;
    console.log(v.detail);
    let number = v.detail;
    let res = await cart_update({
      method:"POST",
      data:{
        goodsId,id,number,productId
      }
    })
    console.log(res);
    let cartList = res.data.data.cartList;
    let cartTotal = res.data.data.cartTotal;
    // console.log(cartTotal);
    this.setData({
      cartList,cartTotal
    })
  },
  // 编辑按钮
  edit(){
    this.setData({show:!this.data.show})
  },
  // 删除
  async del(v){
    console.log(v.currentTarget.dataset.id);
    let productIds = String(v.currentTarget.dataset.id);
    let res_del = await cart_delete({
      header:{
        "X-Nideshop-Token":wx.getStorageSync('token')
      },
      method:"POST",
      data:{
        productIds
      }
    });
    console.log("删除购物车",res_del);
    let {cartList} = res_del.data.data;
    this.setData({cartList})
  },
  // 单个商品多选框
  onChange_item(event) {
    let productIds = event.currentTarget.dataset.id;
    let isChecked = event.detail?1:0;
    this.cartChecked(isChecked,productIds);
    console.log(this.data.checked);
    // console.log(event);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function(options) {
   this.cartIndex();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 加载购物车信息
  async cartIndex(){
    let res = await cart_index({
      header:{
        "X-Nideshop-Token":wx.getStorageSync('token')
      }
    })
    console.log("购物车信息",res);
    if (res.data.errno == 0) {
      let {cartList,cartTotal} = res.data.data;
      this.setData({
        cartList,cartTotal
      })
      for (let i = 0; i < cartList.length; i++) {
        if (!cartList[i].checked) {
          this.setData({checked:false})
          break
        }else{
          this.setData({checked:true})
        }
        }
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function() {
    this.getTabBar().setData({
			active:3
    })
   this.cartIndex();
  
  //  console.log(token);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})